﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLayer
{
    public partial class Car
    {
        public static Car FindByCarID_IncludeDeleted(linqDatabaseDataContext db, int CarID)
        {
            var subquery = (from u in db.Cars where u.ID == (from v in db.Cars where v.CarID == u.CarID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where u.CarID == CarID select u).SingleOrDefault());
        }

        public static Car FindByCarID(linqDatabaseDataContext db, int CarID)
        {
            var subquery = (from u in db.Cars where u.ID == (from v in db.Cars where v.CarID == u.CarID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted && u.CarID == CarID select u).SingleOrDefault());
        }

        public static List<Car> ToList_IncludeDeleted(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Cars where u.ID == (from v in db.Cars where v.CarID == u.CarID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery select u).ToList());
        }

        public static List<Car> ToList(linqDatabaseDataContext db)
        {
            var subquery = (from u in db.Cars where u.ID == (from v in db.Cars where v.CarID == u.CarID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where !u.IsDeleted select u).ToList());
        }

        public static List<Car> ToListByRequestID_IncludeDeleted(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.Cars where u.ID == (from v in db.Cars where v.CarID == u.CarID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where (from v in db.sCars where v.RequestID == RequestID select v.ID).Contains(u.CarID) select u).ToList());
        }

        public static List<Car> ToListByRequestID(linqDatabaseDataContext db, int RequestID)
        {
            var subquery = (from u in db.Cars where u.ID == (from v in db.Cars where v.CarID == u.CarID orderby v.Timestamp descending select v).FirstOrDefault().ID select u);
            return ((from u in subquery where (from v in db.sCars where v.RequestID == RequestID select v.ID).Contains(u.CarID) && !u.IsDeleted select u).ToList());
        }

        public static List<Car> HistoryToListByCarID(linqDatabaseDataContext db, int CarID)
        {
            return (from u in db.Cars where u.CarID == CarID orderby u.Timestamp ascending select u).ToList();
        }
    }
}
